package org.mbds.android.p2p.threads;

import org.mbds.android.p2p.services.P2pService;

import android.net.wifi.WpsInfo;
import android.net.wifi.p2p.WifiP2pConfig;
import android.net.wifi.p2p.WifiP2pDevice;
import android.net.wifi.p2p.WifiP2pManager;
import android.net.wifi.p2p.WifiP2pManager.Channel;
import android.os.AsyncTask;
import android.widget.Toast;

public class ConnectToAPeer extends AsyncTask<WifiP2pDevice, Boolean, Boolean>{
	
	WifiP2pManager mManager;
	Channel mChannel;
	P2pService mService;
	
	public ConnectToAPeer(WifiP2pManager manager, Channel channel, P2pService service){
		mManager = manager;
		mChannel =  channel;
		mService = service;
	}
	@Override
	protected Boolean doInBackground(WifiP2pDevice... params) {
		
		WifiP2pDevice dev = params[0];
		WifiP2pConfig config = new WifiP2pConfig();
		config.deviceAddress = dev.deviceAddress;
		config.groupOwnerIntent = 15;
		try {
			Thread.sleep(5000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		mManager.connect(mChannel, config, new WifiP2pManager.ActionListener() {
			
			@Override
			public void onSuccess() {
				publishProgress(true);
			}
			
			@Override
			public void onFailure(int reason) {
				publishProgress(false);
			}
		});
		return null;
	}
	
	@Override
	protected void onProgressUpdate(Boolean...booleans){
		mService.connexionEstablished(booleans[0]);
	}
	@Override
	protected void onPostExecute(Boolean result){
		//mService.connexionEstablished(result);
	}
}
